package com.iqiyi.pps.epg.web.timer;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.model.program.ProgramDirector;
import com.iqiyi.pps.epg.core.service.program.IProgramAsyncService;
import com.iqiyi.pps.epg.core.service.program.IProgramDirectorResourceService;
import com.iqiyi.pps.epg.core.service.program.IProgramDirectorService;
import com.iqiyi.pps.epg.core.utils.ApiDirectorUtil;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2017/4/20.
 */
public class DirectorRecycleTrigger {
    private static Logger logger = LoggerFactory.getLogger(DirectorRecycleTrigger.class);
    public static final int DIRECTOR_RECYCLE_GAP_MINUTE = Configuration.getInt("director.recycle.gap.minute");
    @Autowired
    private IProgramDirectorService programDirectorService;

    @Autowired
    private IProgramDirectorResourceService programDirectorResourceService;

    @Autowired
    private IProgramAsyncService asyncService;


    public void task() {
        logger.info("[DirectorRecycleTrigger][task] begin");
        Map<String, String> params = new HashMap<String, String>();
        long actualTime = System.currentTimeMillis() - DateTimeUtil.MINUTE_TIME * DIRECTOR_RECYCLE_GAP_MINUTE ;
        List<Criterion> criterionList = new ArrayList<Criterion>();
        criterionList.add(Restrictions.lt("stopPlayTime", new Timestamp( actualTime)));
        criterionList.add(Restrictions.between("playStatus", 0,2));
        List<ProgramDirector> programDirectors = programDirectorService.findProgramDirectorList(criterionList);
        for (ProgramDirector programDirector : programDirectors) {
            notifyRelease(programDirector);
        }
        logger.info("[DirectorRecycleTrigger][task] end");
    }

    private void notifyRelease(ProgramDirector programDirector) {
        Map<String, String> params1 = new HashMap<>();
        params1.put("PID", String.valueOf(programDirector.getProgramQipuId()));
        JSONObject result1 = ApiDirectorUtil.doReleaseDirector(params1);
        if (result1 != null && Constants.CODE_SUC.equals(result1.get(ApiDirectorUtil.ERROR_CODE))) {
            programDirectorResourceService.cleanResourceByDirectorId(programDirector.getId());
            programDirector.setPlayStatus(ProgramDirector.TERMINATE_PLAY);
            programDirectorService.updateProgramDirector(programDirector);
            asyncService.addRecycleStreamUrl(programDirector);
            logger.info("[SUC] release [id={}][qipuId={}]", programDirector.getId(), programDirector.getProgramQipuId());
        } else {
            logger.info("[FAIL] release [id={}][qipuId={}]", programDirector.getId(), programDirector.getProgramQipuId());
        }
    }
}
